package com.jungle.client.demo;


import com.jungle.client.common.MyObservableValue;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


public class TreeTableDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建 TreeTableView
        TreeTableView<Person> treeTableView = new TreeTableView<>();
        TreeTableColumn<Person, Integer> rowNumberColumn = new TreeTableColumn<>("#");
        rowNumberColumn.setPrefWidth(50);
        //rowNumberColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().getRowNumber());
        rowNumberColumn.setCellFactory(column -> new TreeTableCell<>() {
            @Override
            protected void updateItem(Integer item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    setText(String.valueOf(getTableRow().getIndex() + 1));
                }
            }
        });
        // 创建 TreeTableColumn
        TreeTableColumn<Person, String> firstNameColumn = new TreeTableColumn<>("First Name");
        TreeTableColumn<Person, String> lastNameColumn = new TreeTableColumn<>("Last Name");
        TreeTableColumn<Person, Integer> ageColumn = new TreeTableColumn<>("Age");

        // 设置列
        treeTableView.getColumns().add(firstNameColumn);
        treeTableView.getColumns().add(rowNumberColumn);
        treeTableView.getColumns().add(lastNameColumn);
        treeTableView.getColumns().add(ageColumn);


        // 设置列的单元格工厂
        firstNameColumn.setCellValueFactory(cellData->new MyObservableValue<>(cellData, Person::getFirstName));
        lastNameColumn.setCellValueFactory(cellData->new MyObservableValue<>(cellData, Person::getLastName));
        ageColumn.setCellValueFactory(cellData->new MyObservableValue<>(cellData, Person::getAge));

        // 设置列的单元格编辑器
        firstNameColumn.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());
        lastNameColumn.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());
        ageColumn.setCellFactory(list -> new TextFieldTreeTableCell<>(new StringConverter<>() {
            @Override
            public String toString(Integer object) {
                return object == null ? "" : object.toString();
            }

            @Override
            public Integer fromString(String string) {
                return Integer.parseInt(string);
            }
        }));

        // 创建树形数据
        TreeItem<Person> root = new TreeItem<>(new Person("John", "Doe", 30,null));
        TreeItem<Person> child1 = new TreeItem<>(new Person("Jane", "Doe", 25,null));
        TreeItem<Person> child2 = new TreeItem<>(new Person("Joe", "Bloggs", 20,null));
        TreeItem<Person> child3 = new TreeItem<>(new Person("Joe3", "Bloggs3", 20,null));
        TreeItem<Person> child4 = new TreeItem<>(new Person("Joe4", "Bloggs4", 20,null));
        root.getChildren().add(child1);
        child1.getChildren().add(child2);
        child1.getChildren().add(child3);
        child3.getChildren().add(child4);

        // 设置 TreeTableView 的根
        treeTableView.setRoot(root);

        // 创建场景并设置舞台
        VBox vbox = new VBox(treeTableView);
        Scene scene = new Scene(vbox, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeTableView Demo");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    // 简单的 Person 类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Person {
        private String firstName;
        private String lastName;
        private Integer age;
        private Integer rowNumber;
    }
}

